package com.nervenets.web.redis;

import com.alibaba.fastjson.JSON;
import com.nervenets.utils.JodaUtils;
import com.nervenets.web.enumeration.Time;
import com.nervenets.web.redis.domain.global.GlobalUserCount;
import com.nervenets.web.redis.domain.global.GlobalRule;
import org.springframework.stereotype.Service;
import org.weixin4j.WeixinException;
import org.weixin4j.http.OAuthToken;
import redis.clients.jedis.JedisCommands;

import javax.annotation.Resource;

/**
 * Created by Joe on 2018/1/6.
 */
@Service
public class GlobalVariablesService {
    private static final int _OPERATION_INTERVAL_SECONDS_ = 3;
    private static final String _OPERATION_INTERVAL_KEY_ = "_LC_INTERVAL_%s_";
    private static final String _OPERATION_CUSTOM_INTERVAL_KEY_ = "_LC_OPERATION_CUSTOM_INTERVAL_KEY_%s_";
    private static final String _WE_CHAT_AUTH_TOKEN_ = "_LC_WE_CHAT_AUTH_TOKEN_";
    private static final String _SESSION_ = "_LC_SESSION_%s_%s_";
    @Resource
    private RedisDao redisDao;
    @Resource
    private RedisSetDao redisSetDao;
    @Resource
    private RedisOrderedSetDao redisOrderedSetDao;


    /**
     * 用户能否在操作时间间隔内继续操作
     *
     * @param userId 用户ID
     * @return 能否继续操作
     */
    public boolean canProceed(long userId) {
        String proKey = String.format(_OPERATION_INTERVAL_KEY_, userId);
        JedisCommands jedisCluster = redisDao.getCluster();
        if (jedisCluster.setnx(proKey, proKey) == 1) {
            jedisCluster.expire(proKey, _OPERATION_INTERVAL_SECONDS_);
            return true;
        } else {
            return false;
        }
    }

    public boolean canProceedCustom(String key, int expire) {
        String proKey = String.format(_OPERATION_CUSTOM_INTERVAL_KEY_, key);
        JedisCommands jedisCluster = redisDao.getCluster();
        if (jedisCluster.setnx(proKey, proKey) == 1) {
            jedisCluster.expire(proKey, expire);
            return true;
        } else {
            return false;
        }
    }

    public void setWeChatAuthToken(OAuthToken oAuthToken) {
        JedisCommands jedisCluster = redisDao.getCluster();
        jedisCluster.setex(_WE_CHAT_AUTH_TOKEN_, 7000, oAuthToken.toString());
    }

    public OAuthToken getWeChatAuthToken() throws WeixinException {
        JedisCommands jedisCluster = redisDao.getCluster();
        if (jedisCluster.exists(_WE_CHAT_AUTH_TOKEN_)) {
            String tokenJson = jedisCluster.get(_WE_CHAT_AUTH_TOKEN_);
            return new OAuthToken(JSON.parseObject(tokenJson));
        }
        return null;
    }

    public void setSession(long userId, String key, String value) {
        setSession(userId, key, value, 600);
    }

    public void setSession(long userId, String key, String value, int expired) {
        JedisCommands jedisCommands = redisDao.getCluster();
        String redisKey = String.format(_SESSION_, userId, key);
        jedisCommands.set(redisKey, value);
        if (expired > 0) {
            jedisCommands.expire(redisKey, expired);
        }
    }

    public String getSession(long userId, String key) {
        String redisKey = String.format(_SESSION_, userId, key);
        JedisCommands jedisCommands = redisDao.getCluster();
        if (jedisCommands.exists(redisKey)) {
            return jedisCommands.get(redisKey);
        }
        return null;
    }

    public Long getSessionExpired(long userId, String key) {
        String redisKey = String.format(_SESSION_, userId, key);
        JedisCommands jedisCommands = redisDao.getCluster();
        if (jedisCommands.exists(redisKey)) {
            return jedisCommands.ttl(redisKey);
        }
        return null;
    }

    public GlobalRule getGlobalRule() {
        GlobalRule rule = new GlobalRule();
        if (redisDao.exist(rule.getRedisKey())) {
            rule = redisDao.getRedisDomain(rule.getRedisKey(), GlobalRule.class);
        }
        return rule;
    }

    public void setGlobalRule(GlobalRule globalRule) {
        redisDao.saveOrUpdate(globalRule);
    }
}
